using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PluginHandler.Database;
using DatabaseManager.Util;

namespace DatabaseManager.UI
{
    /// <summary>
    /// The form, which is used to generate a query.
    /// </summary>
    public partial class FGenerateQuery : Form
    {
        private IEnumerable<IDatabaseTableColumn> _databaseTableColumns;
        private IDatabaseObject _databaseObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="FGenerateQuery"/> class.
        /// </summary>
        public FGenerateQuery()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvItem in lvAvailableColumns.SelectedItems)
            {
                lvColumnsToGenerate.Items.Add((ListViewItem)lvItem.Clone());
            }

            lvAvailableColumns.SelectedItems.Clear();
            lvAvailableColumns.Select();
        }

        /// <summary>
        /// Gets or sets the database columns, which can be choosen in the query designer for creating a statement.
        /// The list of colums will be filled, when the database columns enumeration will be assigned to this property.
        /// </summary>
        /// <value>The database columns.</value>
        public IEnumerable<IDatabaseTableColumn> DatabaseColumns
        {
            get { return _databaseTableColumns; }
            set 
            { 
                _databaseTableColumns = value;

                lvAvailableColumns.Items.Clear();
                lvColumnsToGenerate.Items.Clear();

                //Won't work with DelimitedIdentifiers. Code needs to be redesigned.
                //lvAvailableColumns.Items.Add("*");

                foreach (IDatabaseTableColumn databaseColumn in _databaseTableColumns)
                { 
                    ListViewItem lvItem = lvAvailableColumns.Items.Add(databaseColumn.Name);
                    lvItem.SubItems.Add(databaseColumn.NativeDbTypeName);
                }
            }
        }

        /// <summary>
        /// Gets or sets the database object, which columns are displayed an can be choosen in the query designer.
        /// </summary>
        /// <value>The database object.</value>
        public IDatabaseObject DatabaseObject
        {
            get { return _databaseObject; }
            set
            {
                _databaseObject = value;
            }
        }

        /// <summary>
        /// Gets the selected columns.
        /// </summary>
        /// <value>The selected columns.</value>
        public string[] SelectedColumns
        {
            get
            {
                string[] columnsToGenerate = new string[lvColumnsToGenerate.Items.Count];

                for (int i = 0; i < lvColumnsToGenerate.Items.Count; i++)
                {
                    columnsToGenerate[i] = lvColumnsToGenerate.Items[i].Text;
                }

                return columnsToGenerate;
            }
        }

        private void btnRemoveSelectedColumns_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvItem in lvColumnsToGenerate.SelectedItems)
            {
                lvItem.Remove();
            }
        }

        private void lvAvailableColumns_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnAdd_Click(btnAdd, EventArgs.Empty);
        }

        private void FGenerateQuery_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            Help.ShowHelp(
                this,
                Utility.GetHelpFileName(),
                HelpNavigator.TopicId,
                HelpTopics.FunctionsGenerateQuery);

            e.Cancel = true;
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            Utility.MoveListViewItems(lvColumnsToGenerate, Utility.Direction.Up);
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            Utility.MoveListViewItems(lvColumnsToGenerate, Utility.Direction.Down);
        }
    }
}